<?php

namespace app\nideshop\controller;

use think\Db;
use app\nideshop\service\Token;

/**
 * 
 */
class Comment extends Base
{
    protected $datas;

    public function postAction()
    {
        $this->datas = $this->params;
        $typeId = $this->datas['typeId'];
        $valueId = $this->datas['valueId'];
        $content = $this->datas['content'];
        $userId = Token::unlock_url($_SERVER['HTTP_X_NIDESHOP_TOKEN']);


        $insertId = Db::name('comment')
        ->insertGetId([
            'type_id' => $typeId,
            'value_id' => $valueId,
            'content' => base64_encode($content),
            'add_time' => time(),
            'user_id' => $userId
        ]);

        if (!empty($insertId))
        {
            return json([
                'errno' => 0,
                'errmsg' => '评论添加成功'
            ]);
        }
        else
        {
            return json([
                'errno' => 1,
                'errmsg' => '评论保存失败'
            ]);
        }
    }

    public function countAction()
    {
        $this->datas = $this->params;
        $typeId = $this->datas['typeId'];
        $valueId = $this->datas['valueId'];

        $allCount = Db::name('comment')
        ->where('type_id', $typeId)
        ->where('value_id', $valueId)
        ->count('id');

        $hasPicCount = Db::name('comment')
        ->alias('comment')
        ->join('comment_picture comment_picture', 'comment.id=comment_picture.comment_id', 'right')
        ->where('comment.type_id', $typeId)
        ->where('comment.value_id', $valueId)
        ->count('comment.id');

        return json([
            'errno' => 0,
			'errmsg' => '',
			'data' => [
                'allCount' => $allCount,
                'hasPicCount' => $hasPicCount
            ]
        ]);
    }

    public function listAction()
    {
        $this->datas = $this->params;
        $typeId = $this->datas['typeId'];
        $valueId = $this->datas['valueId'];
        $showType = 0; // 选择评论的类型 0 全部， 1 只显示图片
        $page = 0;
        $size = $this->datas['size'];

        if (isset($this->datas['showType'])) 
        {
            $showType = $this->datas['showType'];
        }

        if (isset($this->datas['page'])) 
        {
            $page = $this->datas['page'];
        }


        $comments = [];
        if ($showType !== 1)
        {
            $comments = Db::name('comment')
            ->where('type_id', $typeId)
            ->where('value_id', $valueId)
            ->paginate($size, false, ['page' => $page]);
        }
        else
        {
            $comments = Db::name('comment')
            ->alias('comment')
            ->field('comment.*')
            ->join('comment_picture comment_picture', 'comment.id=comment_picture.comment_id', 'right')
            ->where('comment.type_id', $typeId)
            ->where('comment.value_id', $valueId)
            ->paginate($size, false, ['page' => $page]);
        }
        $commentList = [];
        $commentInfo = $comments->items();
        for ($i = 0; $i < count($commentInfo); $i++)
        {
            $commentItem = $commentInfo[$i];
            $comment = [];
            $comment['content'] = base64_decode($commentItem['content']);
            $comment['type_id'] = $commentItem['type_id'];
            $comment['value_id'] = $commentItem['value_id'];
            $comment['id'] = $commentItem['id'];
            $comment['add_time'] = date( "Y-m-d H:i", $commentItem['add_time']);

            $comment['user_info'] = 
            Db::name('user')
            ->field('username, avatar, nickname')
            ->where('id', $commentItem['user_id'])
            ->find();

            $comment['pic_list'] = 
            Db::name('comment_picture')
            ->where('comment_id', $commentItem['id'])
            ->select();
            array_push($commentList, $comment);
        }

        return json([
            'errno' => 0,
			'errmsg' => '',
			'data' => [
                'count' => $comments->total(),
                'totalPages' => $comments->lastPage(),
                'pageSize' => (int) $size,
                'currentPage' => $page,
				'data' => $commentList
			]
        ]);
    }
}
